<!DOCTYPE html>


  <html class="dark page-post">


<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  
  <title>使用umi改进dva项目开发 | Poetry&#39;s Blog</title>

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  
    <meta name="keywords" content="Dva,Umi,">
  

  <meta name="description" content="一、Umi简介 一个可插拔的企业级 react 应用框架。umi 以路由为基础的，以及各种进阶的路由功能，并以此进行功能扩展，比如支持路由级的按需加载。然后配以完善的插件体系，覆盖从源码到构建产物的每个生命周期  1.1 特性 开箱即用，内置 react、react-router 等 支持配置的路由方式 完善的插件体系，覆盖从源码到构建产物的每个生命周期 高性能，通过插件支持 PWA、以路由为单元">
<meta name="keywords" content="Dva,Umi">
<meta property="og:type" content="article">
<meta property="og:title" content="使用umi改进dva项目开发">
<meta property="og:url" content="http://blog.poetries.top/2018/09/07/umi-dva/index.html">
<meta property="og:site_name" content="Poetry&#39;s Blog">
<meta property="og:description" content="一、Umi简介 一个可插拔的企业级 react 应用框架。umi 以路由为基础的，以及各种进阶的路由功能，并以此进行功能扩展，比如支持路由级的按需加载。然后配以完善的插件体系，覆盖从源码到构建产物的每个生命周期  1.1 特性 开箱即用，内置 react、react-router 等 支持配置的路由方式 完善的插件体系，覆盖从源码到构建产物的每个生命周期 高性能，通过插件支持 PWA、以路由为单元">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://gw.alipayobjects.com/zos/rmsportal/zvfEXesXdgTzWYZCuHLe.png">
<meta property="og:image" content="https://gw.alipayobjects.com/zos/rmsportal/mlEDcowMOSeXwLoukayR.png">
<meta property="og:image" content="https://gw.alipayobjects.com/zos/rmsportal/ppRAiFpnZbpwDDuoFdPh.png">
<meta property="og:image" content="https://gw.alipayobjects.com/zos/rmsportal/wbeiDacBkchXrTafasBy.png">
<meta property="og:updated_time" content="2020-08-15T04:25:31.938Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="使用umi改进dva项目开发">
<meta name="twitter:description" content="一、Umi简介 一个可插拔的企业级 react 应用框架。umi 以路由为基础的，以及各种进阶的路由功能，并以此进行功能扩展，比如支持路由级的按需加载。然后配以完善的插件体系，覆盖从源码到构建产物的每个生命周期  1.1 特性 开箱即用，内置 react、react-router 等 支持配置的路由方式 完善的插件体系，覆盖从源码到构建产物的每个生命周期 高性能，通过插件支持 PWA、以路由为单元">
<meta name="twitter:image" content="https://gw.alipayobjects.com/zos/rmsportal/zvfEXesXdgTzWYZCuHLe.png">

  

  
    <link rel="icon" href="/favicon.ico">
  

  <link href="/css/styles.css?v=c114cbeddx" rel="stylesheet">
<link href="/css/other.css?v=c114cbeddx" rel="stylesheet">


  
    <link rel="stylesheet" href="/css/personal-style.css">
  

  

  
  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?40b1f89aa80f2527b3db779c6898c879";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>


  
  <script type="text/javascript">
	(function(){
	    var bp = document.createElement('script');
	    var curProtocol = window.location.protocol.split(':')[0];
	    if (curProtocol === 'https') {
	        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
	    }
	    else {
	        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
	    }
	    var s = document.getElementsByTagName("script")[0];
	    s.parentNode.insertBefore(bp, s);
	})();
  </script>



  
    <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/4.3.0/css/font-awesome.min.css">
  

  <!-- 聊天系统 -->
  
    
   <link type="text/css" rel="stylesheet" href="/renxi/default.css">
   <style>
      #modal {
        position: static !important;
      }
      .filter {
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
        background: #fe5757;
        animation: colorChange 30s ease-in-out infinite;
        animation-fill-mode: both;
        mix-blend-mode: overlay;
      }
  
      @keyframes colorChange {
        0%, 100% {
            opacity: 0;
        }
        50% {
            opacity: .9;
        }
      }
   </style>
</head>
</html>
<body>
  
  
    <span id="toolbox-mobile" class="toolbox-mobile">导航</span>
  

  <div class="post-header CENTER">
   
  <div class="toolbox">
    <a class="toolbox-entry" href="/">
      <span class="toolbox-entry-text">导航</span>
      <i class="icon-angle-down"></i>
      <i class="icon-home"></i>
    </a>
    <ul class="list-toolbox">
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/archives/"
            rel="noopener noreferrer"
            target="_self"
            >
            博客
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/categories/"
            rel="noopener noreferrer"
            target="_self"
            >
            分类
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/tags/"
            rel="noopener noreferrer"
            target="_self"
            >
            标签
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/search/"
            rel="noopener noreferrer"
            target="_self"
            >
            搜索
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/link/"
            rel="noopener noreferrer"
            target="_self"
            >
            友链
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/about/"
            rel="noopener noreferrer"
            target="_self"
            >
            关于
          </a>
        </li>
      
    </ul>
  </div>


</div>


  <div id="toc" class="toc-article">
    <strong class="toc-title">文章目录<i class="iconfont toc-title" style="display:inline-block;color:#87998d;width:20px;height:20px;">&#xf004b;</i></strong>
    <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#一、Umi简介"><span class="toc-text">一、Umi简介</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-1-特性"><span class="toc-text">1.1 特性</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-2-架构"><span class="toc-text">1.2 架构</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-3-和-dva、roadhog关系"><span class="toc-text">1.3 和 dva、roadhog关系</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#二、环境搭建"><span class="toc-text">二、环境搭建</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#三、目录结构"><span class="toc-text">三、目录结构</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#四、路由配置"><span class="toc-text">四、路由配置</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#4-1-约定式路由"><span class="toc-text">4.1 约定式路由</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#4-1-1-基础路由"><span class="toc-text">4.1.1 基础路由</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-1-2-动态路由"><span class="toc-text">4.1.2 动态路由</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-1-3-可选的动态路由"><span class="toc-text">4.1.3 可选的动态路由</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-1-4-嵌套路由"><span class="toc-text">4.1.4 嵌套路由</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-1-5-全局-layout"><span class="toc-text">4.1.5 全局 layout</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-1-6-不同的全局-layout"><span class="toc-text">4.1.6 不同的全局 layout</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-1-7-404-路由"><span class="toc-text">4.1.7 404 路由</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-1-8-通过注释扩展路由"><span class="toc-text">4.1.8 通过注释扩展路由</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-2-配置式路由"><span class="toc-text">4.2 配置式路由</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-3-权限路由"><span class="toc-text">4.3 权限路由</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-4-路由动效"><span class="toc-text">4.4 路由动效</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-5-面包屑"><span class="toc-text">4.5 面包屑</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-6-启用-Hash-路由"><span class="toc-text">4.6 启用 Hash 路由</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-7-页面间跳转"><span class="toc-text">4.7 页面间跳转</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#五、配置"><span class="toc-text">五、配置</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#六、Mock-数据"><span class="toc-text">六、Mock 数据</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#七、结合dva实践"><span class="toc-text">七、结合dva实践</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#八、问题汇总"><span class="toc-text">八、问题汇总</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#九、Demo"><span class="toc-text">九、Demo</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#十、参考"><span class="toc-text">十、参考</span></a></li></ol>
  </div>
  




<div class="content content-post CENTER">
   <!-- canvas 彩带 -->
<canvas id="evanyou" width="1302" height="678" style="position: fixed;width: 100%;height: 100%;top: 0;left:0;z-index:-1;"></canvas>

<article id="post-umi-dva" class="article article-type-post" itemprop="blogPost">
  <header class="article-header" style="position:relative;">
    <h1 class="post-title">使用umi改进dva项目开发</h1>

    <div class="article-meta">
      <span>
        <i class="icon-calendar"></i>
        <span>2018.09.07</span>
      </span>

      
        <span class="article-author">
          <i class="icon-user"></i>
          <span>Poetry</span>
        </span>
      

      
  <span class="article-category">
    <i class="icon-list"></i>
    <a class="article-category-link" href="/categories/Front-End/">Front-End</a>
  </span>



      

      
      <i class="fa fa-eye"></i> 
        <span id="busuanzi_container_page_pv">
           &nbsp热度 <span id="busuanzi_value_page_pv">
           <i class="fa fa-spinner fa-spin"></i></span>℃
        </span>
      
      
       
          <span class="post-count">
            <i class="fa fa-file-word-o"></i>&nbsp
            <span>字数统计 4.4k字</span>
          </span>

          <span class="post-count">
            <i class="fa fa-columns"></i>&nbsp
            <span>阅读时长 19分</span>
          </span>
      
      
    </div>

    <i class="iconfont" id="toc-eye" style="display:inline-block;color:#b36619;position:absolute;top:0;right:0;cursor:pointer;
    font-size: 24px;">&#xe61c;</i>

  </header>

  <div class="article-content">
    
      <div id="container">
        <h1 id="一、Umi简介"><a href="#一、Umi简介" class="headerlink" title="一、Umi简介"></a>一、Umi简介</h1><blockquote>
<p>一个可插拔的企业级 <code>react</code> 应用框架。<code>umi</code> 以路由为基础的，以及各种进阶的路由功能，并以此进行功能扩展，比如支持路由级的按需加载。然后配以完善的插件体系，覆盖从源码到构建产物的每个生命周期</p>
</blockquote>
<h2 id="1-1-特性"><a href="#1-1-特性" class="headerlink" title="1.1 特性"></a>1.1 特性</h2><ul>
<li>开箱即用，内置 <code>react</code>、<code>react-router</code> 等</li>
<li>支持配置的路由方式</li>
<li>完善的插件体系，覆盖从源码到构建产物的每个生命周期</li>
<li>高性能，通过插件支持 <code>PWA</code>、以路由为单元的 <code>code splitting</code> 等</li>
<li>支持静态页面导出，适配各种环境，比如中台业务、无线业务、egg、支付宝钱包、云凤蝶等</li>
<li>开发启动快，支持一键开启 <code>dll</code> 和 <code>hard-source-webpack-plugin</code> 等</li>
<li>一键兼容到 <code>IE9</code>，基于 <code>umi-plugin-polyfills</code></li>
<li>完善的 <code>TypeScript</code> 支持，包括 <code>d.ts</code>定义和 <code>umi test</code></li>
<li>与 <code>dva</code> 数据流的深入融合，支持 <code>duck directory</code>、<code>model</code> 的自动加载、<code>code splitting</code>等等</li>
</ul>
<h2 id="1-2-架构"><a href="#1-2-架构" class="headerlink" title="1.2 架构"></a>1.2 架构</h2><p><img src="https://gw.alipayobjects.com/zos/rmsportal/zvfEXesXdgTzWYZCuHLe.png" alt></p>
<h2 id="1-3-和-dva、roadhog关系"><a href="#1-3-和-dva、roadhog关系" class="headerlink" title="1.3 和 dva、roadhog关系"></a>1.3 和 dva、roadhog关系</h2><ul>
<li><code>roadhog</code> 是基于 <code>webpack</code> 的封装工具，目的是简化 <code>webpack</code> 的配置<br>umi 可以简单地理解为 <code>roadhog + 路由</code>，思路类似 <code>next.js/nuxt.js</code>，辅以一套插件机制，目的是通过框架的方式简化 <code>React</code>开发</li>
<li><code>dva</code> 目前是纯粹的数据流，和 <code>umi</code> 以及 <code>roadhog</code> 之间并没有相互的依赖关系，可以分开使用也可以一起使用</li>
</ul>
<h1 id="二、环境搭建"><a href="#二、环境搭建" class="headerlink" title="二、环境搭建"></a>二、环境搭建</h1><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">$ mkdir myapp &amp;&amp; cd myapp</span><br><span class="line">$ yarn create umi</span><br></pre></td></tr></table></figure>
<p><img src="https://gw.alipayobjects.com/zos/rmsportal/mlEDcowMOSeXwLoukayR.png" alt></p>
<blockquote>
<p>确定后，会根据你的选择自动创建好目录和文件</p>
</blockquote>
<p><img src="https://gw.alipayobjects.com/zos/rmsportal/ppRAiFpnZbpwDDuoFdPh.png" alt></p>
<h1 id="三、目录结构"><a href="#三、目录结构" class="headerlink" title="三、目录结构"></a>三、目录结构</h1><blockquote>
<p><code>dva</code> 项目之前通常都是这种扁平的组织方式</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">+ models</span><br><span class="line">  - global.js</span><br><span class="line">  - a1.js</span><br><span class="line">  - a2.js</span><br><span class="line">  - b.js</span><br><span class="line">+ services</span><br><span class="line">  - a.js</span><br><span class="line">  - b.js</span><br><span class="line">+ routes</span><br><span class="line">  - PageA.js</span><br><span class="line">  - PageB.js</span><br></pre></td></tr></table></figure>
<blockquote>
<p>用了 <code>umi</code> 后，可以按页面维度进行组织</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">+ models/global.js</span><br><span class="line">+ pages</span><br><span class="line">  + a</span><br><span class="line">    - index.js</span><br><span class="line">    + models</span><br><span class="line">      - a1.js</span><br><span class="line">      - a2.js</span><br><span class="line">    + services</span><br><span class="line">      - a.js</span><br><span class="line">  + b</span><br><span class="line">    - index.js</span><br><span class="line">    - model.js</span><br><span class="line">    - service.js</span><br></pre></td></tr></table></figure>
<blockquote>
<p>好处是更加结构更加清晰了，减少耦合，一删全删，方便 copy 和共享</p>
</blockquote>
<p><strong>自动注册 models</strong></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">+ src</span><br><span class="line">  + models</span><br><span class="line">    - g.js</span><br><span class="line">  + pages</span><br><span class="line">    + a</span><br><span class="line">      + models</span><br><span class="line">        - a.js</span><br><span class="line">        - b.js</span><br><span class="line">        + ss</span><br><span class="line">          - s.js</span><br><span class="line">      - page.js</span><br><span class="line">    + c</span><br><span class="line">      - model.js</span><br><span class="line">      + d</span><br><span class="line">        + models</span><br><span class="line">          - d.js</span><br><span class="line">        - page.js</span><br><span class="line">      - page.js</span><br></pre></td></tr></table></figure>
<ul>
<li><code>global model</code> 为 <code>src/models/g.js</code></li>
<li><code>/a</code> 的 <code>page model</code> 为 <code>src/pages/a/models/{a,b,ss/s}.js</code></li>
<li><code>/c</code> 的 <code>page model</code> 为 <code>src/pages/c/model.js</code></li>
<li><code>/c/d</code> 的 <code>page model</code> 为 <code>src/pages/c/model.js</code>, <code>src/pages/c/d/models/d.js</code></li>
</ul>
<blockquote>
<p>一个复杂应用的目录结构如下</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">.</span><br><span class="line">├── dist/                          <span class="comment">// 默认的 build 输出目录</span></span><br><span class="line">├── mock/                          <span class="comment">// mock 文件所在目录，基于 express</span></span><br><span class="line">├── config/</span><br><span class="line">    ├── config.js                  <span class="comment">// umi 配置，同 .umirc.js，二选一</span></span><br><span class="line">└── src/                           <span class="comment">// 源码目录，可选</span></span><br><span class="line">    ├── layouts/index.js           <span class="comment">// 全局布局</span></span><br><span class="line">    ├── pages/                     <span class="comment">// 页面目录，里面的文件即路由</span></span><br><span class="line">        ├── .umi/                  <span class="comment">// dev 临时目录，需添加到 .gitignore</span></span><br><span class="line">        ├── .umi-production/       <span class="comment">// build 临时目录，会自动删除</span></span><br><span class="line">        ├── <span class="built_in">document</span>.ejs           <span class="comment">// HTML 模板</span></span><br><span class="line">        ├── <span class="number">404.</span>js                 <span class="comment">// 404 页面</span></span><br><span class="line">        ├── page1.js               <span class="comment">// 页面 1，任意命名，导出 react 组件</span></span><br><span class="line">        ├── page1.test.js          <span class="comment">// 用例文件，umi test 会匹配所有 .test.js 和 .e2e.js 结尾的文件</span></span><br><span class="line">        └── page2.js               <span class="comment">// 页面 2，任意命名</span></span><br><span class="line">    ├── global.css                 <span class="comment">// 约定的全局样式文件，自动引入，也可以用 global.less</span></span><br><span class="line">    ├── global.js                  <span class="comment">// 可以在这里加入 polyfill</span></span><br><span class="line">├── .umirc.js                      <span class="comment">// umi 配置，同 config/config.js，二选一</span></span><br><span class="line">├── .env                           <span class="comment">// 环境变量</span></span><br><span class="line">└── package.json</span><br></pre></td></tr></table></figure>
<p><strong>1、dist</strong></p>
<blockquote>
<p>默认输出路径，可通过配置 <code>outputPath</code> 修改</p>
</blockquote>
<p><strong>2、mock</strong></p>
<blockquote>
<p>约定 <code>mock</code>目录里所有的 <code>.js</code> 文件会被解析为 <code>mock</code> 文件</p>
</blockquote>
<p>比如，新建<code>mock/users.js</code>，内容如下：</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  <span class="string">'/api/users'</span>: [<span class="string">'a'</span>, <span class="string">'b'</span>],</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>然后在浏览器里访问 <code>http://localhost:8000/api/users 就可以看到 [&#39;a&#39;, &#39;b&#39;]</code> 了</p>
</blockquote>
<p><strong>3、src</strong></p>
<blockquote>
<p>约定 src 为源码目录，但是可选，简单项目可以不加 src 这层目录</p>
</blockquote>
<p>比如：下面两种目录结构的效果是一致的。</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">+ src</span><br><span class="line">  + pages</span><br><span class="line">    - index.js</span><br><span class="line">  + layouts</span><br><span class="line">    - index.js</span><br><span class="line">- .umirc.js</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">+ pages</span><br><span class="line">  - index.js</span><br><span class="line">+ layouts</span><br><span class="line">  - index.js</span><br><span class="line">- .umirc.js</span><br></pre></td></tr></table></figure>
<p><strong>4、src/layouts/index.js</strong></p>
<blockquote>
<p>全局布局，实际上是在路由外面套了一层</p>
</blockquote>
<p>比如，你的路由是：</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">[</span><br><span class="line">  &#123; <span class="attr">path</span>: <span class="string">'/'</span>, <span class="attr">component</span>: <span class="string">'./pages/index'</span> &#125;,</span><br><span class="line">  &#123; <span class="attr">path</span>: <span class="string">'/users'</span>, <span class="attr">component</span>: <span class="string">'./pages/users'</span> &#125;,</span><br><span class="line">]</span><br></pre></td></tr></table></figure>
<p>如果有 <code>layouts/index.js</code>，那么路由则变为：</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"></span><br><span class="line">[</span><br><span class="line">  &#123; <span class="attr">path</span>: <span class="string">'/'</span>, <span class="attr">component</span>: <span class="string">'./layouts/index'</span>, <span class="attr">routes</span>: [</span><br><span class="line">    &#123; <span class="attr">path</span>: <span class="string">'/'</span>, <span class="attr">component</span>: <span class="string">'./pages/index'</span> &#125;,</span><br><span class="line">    &#123; <span class="attr">path</span>: <span class="string">'/users'</span>, <span class="attr">component</span>: <span class="string">'./pages/users'</span> &#125;,</span><br><span class="line">  ] &#125;</span><br><span class="line">]</span><br></pre></td></tr></table></figure>
<p><strong>5、src/pages</strong></p>
<blockquote>
<p>约定 <code>pages</code> 下所有的 <code>(j|t)sx?</code> 文件即路由</p>
</blockquote>
<p><strong>6、src/pages/404.js</strong></p>
<blockquote>
<p><code>404</code> 页面。注意开发模式下有内置 umi 提供的 404 提示页面，所以只有显式访问 <code>/404</code> 才能访问到这个页面</p>
</blockquote>
<p><strong>7、src/pages/document.ejs</strong></p>
<blockquote>
<p>有这个文件时，会覆盖默认的 HTML 模板。需至少包含以下代码，</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"root"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p><strong>8、src/pages/.umi</strong></p>
<blockquote>
<p>这是 <code>umi dev</code> 时生产的临时目录，默认包含 <code>umi.js</code> 和 <code>router.js</code>，有些插件也会在这里生成一些其他临时文件。可以在这里做一些验证，但请不要直接在这里修改代码，<code>umi</code> 重启或者 <code>pages</code> 下的文件修改都会重新生成这个文件夹下的文件</p>
</blockquote>
<p><strong>9、src/pages/.umi-production</strong></p>
<blockquote>
<p>同 <code>src/pagers/.umi</code>，但是是在 <code>umi build</code>时生成的，会在 <code>umi build</code> 执行完自动删除</p>
</blockquote>
<p><strong>10、src/global.(j|t)sx?</strong></p>
<blockquote>
<p>在入口文件最前面被自动引入，可以考虑在此加入 <code>polyfill</code></p>
</blockquote>
<p><strong>11、src/global.(css|less|sass|scss)</strong></p>
<blockquote>
<p>这个文件不走 <code>css modules</code>，自动被引入，可以写一些全局样式，或者做一些样式覆盖</p>
</blockquote>
<p><strong>12、.umirc.js 和 config/config.js</strong></p>
<blockquote>
<p><code>umi</code> 的配置文件，二选一</p>
</blockquote>
<p><strong>13、.env</strong></p>
<p>环境变量，比如：</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">CLEAR_CONSOLE=none</span><br><span class="line">BROWSER=none</span><br></pre></td></tr></table></figure>
<h1 id="四、路由配置"><a href="#四、路由配置" class="headerlink" title="四、路由配置"></a>四、路由配置</h1><h2 id="4-1-约定式路由"><a href="#4-1-约定式路由" class="headerlink" title="4.1 约定式路由"></a>4.1 约定式路由</h2><h3 id="4-1-1-基础路由"><a href="#4-1-1-基础路由" class="headerlink" title="4.1.1 基础路由"></a>4.1.1 基础路由</h3><p>假设 <code>pages</code> 目录结构如下：</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">+ pages/</span><br><span class="line">  + users/</span><br><span class="line">    - index.js</span><br><span class="line">    - list.js</span><br><span class="line">  - index.js</span><br></pre></td></tr></table></figure>
<p>那么，umi 会自动生成路由配置如下：</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">[</span><br><span class="line">  &#123; <span class="attr">path</span>: <span class="string">'/'</span>, <span class="attr">component</span>: <span class="string">'./pages/index.js'</span> &#125;,</span><br><span class="line">  &#123; <span class="attr">path</span>: <span class="string">'/users/'</span>, <span class="attr">component</span>: <span class="string">'./pages/users/index.js'</span> &#125;,</span><br><span class="line">  &#123; <span class="attr">path</span>: <span class="string">'/users/list'</span>, <span class="attr">component</span>: <span class="string">'./pages/users/list.js'</span> &#125;,</span><br><span class="line">]</span><br></pre></td></tr></table></figure>
<h3 id="4-1-2-动态路由"><a href="#4-1-2-动态路由" class="headerlink" title="4.1.2 动态路由"></a>4.1.2 动态路由</h3><blockquote>
<p><code>umi</code> 里约定，带 <code>$</code> 前缀的目录或文件为动态路由。</p>
</blockquote>
<p>比如以下目录结构：</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">+ pages/</span><br><span class="line">  + $post/</span><br><span class="line">    - index.js</span><br><span class="line">    - comments.js</span><br><span class="line">  + users/</span><br><span class="line">    $id.js</span><br><span class="line">  - index.js</span><br></pre></td></tr></table></figure>
<p>会生成路由配置如下：</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">[</span><br><span class="line">  &#123; <span class="attr">path</span>: <span class="string">'/'</span>, <span class="attr">component</span>: <span class="string">'./pages/index.js'</span> &#125;,</span><br><span class="line">  &#123; <span class="attr">path</span>: <span class="string">'/users/:id'</span>, <span class="attr">component</span>: <span class="string">'./pages/users/$id.js'</span> &#125;,</span><br><span class="line">  &#123; <span class="attr">path</span>: <span class="string">'/:post/'</span>, <span class="attr">component</span>: <span class="string">'./pages/$post/index.js'</span> &#125;,</span><br><span class="line">  &#123; <span class="attr">path</span>: <span class="string">'/:post/comments'</span>, <span class="attr">component</span>: <span class="string">'./pages/$post/comments.js'</span> &#125;,</span><br><span class="line">]</span><br></pre></td></tr></table></figure>
<h3 id="4-1-3-可选的动态路由"><a href="#4-1-3-可选的动态路由" class="headerlink" title="4.1.3 可选的动态路由"></a>4.1.3 可选的动态路由</h3><blockquote>
<p><code>umi</code> 里约定动态路由如果带 <code>$</code> 后缀，则为可选动态路由。</p>
</blockquote>
<p>比如以下结构：</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">+ pages/</span><br><span class="line">  + users/</span><br><span class="line">    - $id$.js</span><br><span class="line">  - index.js</span><br></pre></td></tr></table></figure>
<p>会生成路由配置如下：</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">[</span><br><span class="line">  &#123; <span class="attr">path</span>: <span class="string">'/'</span>: component: <span class="string">'./pages/index.js'</span> &#125;,</span><br><span class="line">  &#123; <span class="attr">path</span>: <span class="string">'/users/:id?'</span>: component: <span class="string">'./pages/users/$id$.js'</span> &#125;,</span><br><span class="line">]</span><br></pre></td></tr></table></figure>
<h3 id="4-1-4-嵌套路由"><a href="#4-1-4-嵌套路由" class="headerlink" title="4.1.4 嵌套路由"></a>4.1.4 嵌套路由</h3><blockquote>
<p><code>umi</code> 里约定目录下有<code>_layout.js</code> 时会生成嵌套路由，以<code>_layout.js</code> 为该目录的 <code>layout</code> 。</p>
</blockquote>
<p>比如以下目录结构：</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">+ pages/</span><br><span class="line">  + users/</span><br><span class="line">    - _layout.js</span><br><span class="line">    - $id.js</span><br><span class="line">    - index.js</span><br></pre></td></tr></table></figure>
<p>会生成路由配置如下：</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">[</span><br><span class="line">  &#123; <span class="attr">path</span>: <span class="string">'/users'</span>: component: <span class="string">'./pages/users/_layout.js'</span></span><br><span class="line">    routes: [</span><br><span class="line">     &#123; <span class="attr">path</span>: <span class="string">'/users/'</span>, <span class="attr">component</span>: <span class="string">'./pages/users/index.js'</span> &#125;,</span><br><span class="line">     &#123; <span class="attr">path</span>: <span class="string">'/users/:id'</span>, <span class="attr">component</span>: <span class="string">'./pages/users/$id.js'</span> &#125;,</span><br><span class="line">   ],</span><br><span class="line">  &#125;,</span><br><span class="line">]</span><br></pre></td></tr></table></figure>
<h3 id="4-1-5-全局-layout"><a href="#4-1-5-全局-layout" class="headerlink" title="4.1.5 全局 layout"></a>4.1.5 全局 layout</h3><blockquote>
<p>约定 <code>src/layouts/index.js</code> 为全局路由，返回一个 React 组件，通过 <code>props.children</code> 渲染子组件。</p>
</blockquote>
<p>比如：</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="function"><span class="keyword">function</span>(<span class="params">props</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    &lt;&gt;</span><br><span class="line">      &lt;Header /&gt;</span><br><span class="line">      &#123; props.children &#125;</span><br><span class="line">      &lt;Footer /&gt;</span><br><span class="line">    &lt;<span class="regexp">/&gt;</span></span><br><span class="line"><span class="regexp">  );</span></span><br><span class="line"><span class="regexp">&#125;</span></span><br></pre></td></tr></table></figure>
<h3 id="4-1-6-不同的全局-layout"><a href="#4-1-6-不同的全局-layout" class="headerlink" title="4.1.6 不同的全局 layout"></a>4.1.6 不同的全局 layout</h3><blockquote>
<p>你可能需要针对不同路由输出不同的全局 <code>layout</code>，<code>umi</code> 不支持这样的配置，但你仍可以在 <code>layouts/index.js</code> 对 <code>location.path</code> 做区分，渲染不同的 <code>layout</code>。</p>
</blockquote>
<ul>
<li>比如想要针对 <code>/login</code> 输出简单布局，</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="function"><span class="keyword">function</span>(<span class="params">props</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">if</span> (props.location.pathname === <span class="string">'/login'</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">SimpleLayout</span>&gt;</span>&#123; props.children &#125;<span class="tag">&lt;/<span class="name">SimpleLayout</span>&gt;</span></span></span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    &lt;&gt;</span><br><span class="line">      &lt;Header /&gt;</span><br><span class="line">      &#123; props.children &#125;</span><br><span class="line">      &lt;Footer /&gt;</span><br><span class="line">    &lt;<span class="regexp">/&gt;</span></span><br><span class="line"><span class="regexp">  );</span></span><br><span class="line"><span class="regexp">&#125;</span></span><br></pre></td></tr></table></figure>
<h3 id="4-1-7-404-路由"><a href="#4-1-7-404-路由" class="headerlink" title="4.1.7 404 路由"></a>4.1.7 404 路由</h3><blockquote>
<p>约定 <code>pages/404.js</code> 为 <code>404</code>页面，需返回 <code>React</code> 组件。</p>
</blockquote>
<p>比如：</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> () =&gt; &#123;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    &lt;div&gt;I am a customized <span class="number">404</span> page&lt;<span class="regexp">/div&gt;</span></span><br><span class="line"><span class="regexp">  );</span></span><br><span class="line"><span class="regexp">&#125;;</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>注意：开发模式下，umi 会添加一个默认的 <code>404</code> 页面来辅助开发，但你仍然可通过精确地访问 <code>/404</code>来验证 404 页面。</p>
</blockquote>
<h3 id="4-1-8-通过注释扩展路由"><a href="#4-1-8-通过注释扩展路由" class="headerlink" title="4.1.8 通过注释扩展路由"></a>4.1.8 通过注释扩展路由</h3><blockquote>
<p>约定路由文件的首个注释如果包含 <code>yaml</code>格式的配置，则会被用于扩展路由。</p>
</blockquote>
<p>比如：</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">+ pages/</span><br><span class="line">  - index.js</span><br></pre></td></tr></table></figure>
<blockquote>
<p>如果<code>pages/index.js</code> 里包含：</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * title: Index Page</span></span><br><span class="line"><span class="comment"> * Routes:</span></span><br><span class="line"><span class="comment"> *   - ./src/routes/a.js</span></span><br><span class="line"><span class="comment"> *   - ./src/routes/b.js</span></span><br><span class="line"><span class="comment"> */</span></span><br></pre></td></tr></table></figure>
<p>则会生成路由配置：</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">[</span><br><span class="line">  &#123; <span class="attr">path</span>: <span class="string">'/'</span>, <span class="attr">component</span>: <span class="string">'./index.js'</span>,</span><br><span class="line">    title: <span class="string">'Index Page'</span>,</span><br><span class="line">    Routes: [ <span class="string">'./src/routes/a.js'</span>, <span class="string">'./src/routes/b.js'</span> ],</span><br><span class="line">  &#125;,</span><br><span class="line">]</span><br></pre></td></tr></table></figure>
<h2 id="4-2-配置式路由"><a href="#4-2-配置式路由" class="headerlink" title="4.2 配置式路由"></a>4.2 配置式路由</h2><blockquote>
<p>如果你倾向于使用配置式的路由，可以配置 <code>routes</code> ，此配置项存在时则不会对 <code>src/pages</code> 目录做约定式的解析。</p>
</blockquote>
<p>比如：</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  routes: [</span><br><span class="line">    &#123; <span class="attr">path</span>: <span class="string">'/'</span>, <span class="attr">component</span>: <span class="string">'./a'</span> &#125;,</span><br><span class="line">    &#123; <span class="attr">path</span>: <span class="string">'/list'</span>, <span class="attr">component</span>: <span class="string">'./b'</span>, <span class="attr">Routes</span>: [<span class="string">'./routes/PrivateRoute.js'</span>] &#125;,</span><br><span class="line">    &#123; <span class="attr">path</span>: <span class="string">'/users'</span>, <span class="attr">component</span>: <span class="string">'./users/_layout'</span>,</span><br><span class="line">      routes: [</span><br><span class="line">        &#123; <span class="attr">path</span>: <span class="string">'/users/detail'</span>, <span class="attr">component</span>: <span class="string">'./users/detail'</span> &#125;,</span><br><span class="line">        &#123; <span class="attr">path</span>: <span class="string">'/users/:id'</span>, <span class="attr">component</span>: <span class="string">'./users/id'</span> &#125;</span><br><span class="line">      ]</span><br><span class="line">    &#125;,</span><br><span class="line">  ],</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>注意：<code>component</code> 是相对于 <code>src/pages</code> 目录的</p>
</blockquote>
<h2 id="4-3-权限路由"><a href="#4-3-权限路由" class="headerlink" title="4.3 权限路由"></a>4.3 权限路由</h2><blockquote>
<p><code>umi</code> 的权限路由是通过配置路由的 <code>Routes</code> 属性来实现。约定式的通过 <code>yaml</code> 注释添加，配置式的直接配上即可。</p>
</blockquote>
<p>比如有以下配置：</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">[</span><br><span class="line">  &#123; <span class="attr">path</span>: <span class="string">'/'</span>, <span class="attr">component</span>: <span class="string">'./pages/index.js'</span> &#125;,</span><br><span class="line">  &#123; <span class="attr">path</span>: <span class="string">'/list'</span>, <span class="attr">component</span>: <span class="string">'./pages/list.js'</span>, <span class="attr">Routes</span>: [<span class="string">'./routes/PrivateRoute.js'</span>] &#125;,</span><br><span class="line">]</span><br></pre></td></tr></table></figure>
<blockquote>
<p>然后 umi 会用<code>./routes/PrivateRoute.js</code>来渲染 <code>/list</code>。</p>
</blockquote>
<p><code>./routes/PrivateRoute.js</code> 文件示例：</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> (props) =&gt; &#123;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">      &lt;div&gt;PrivateRoute (routes/PrivateRoute.js)&lt;<span class="regexp">/div&gt;</span></span><br><span class="line"><span class="regexp">      &#123; props.children &#125;</span></span><br><span class="line"><span class="regexp">    &lt;/</span>div&gt;</span><br><span class="line">  );</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="4-4-路由动效"><a href="#4-4-路由动效" class="headerlink" title="4.4 路由动效"></a>4.4 路由动效</h2><blockquote>
<p>路由动效应该是有多种实现方式，这里举 react-transition-group 的例子。</p>
</blockquote>
<p>先安装依赖，</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">$ yarn add react-transition-group</span><br></pre></td></tr></table></figure>
<blockquote>
<p>在 <code>layout</code>  组件（<code>layouts/index.js</code>  或者 <code>pages</code> 子目录下的 _layout.js）里在渲染子组件时用 <code>TransitionGroup</code> 和 <code>CSSTransition</code> 包裹一层，并以 <code>location.key</code> 为 <code>key</code>，</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> withRouter <span class="keyword">from</span> <span class="string">'umi/withRouter'</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; TransitionGroup, CSSTransition &#125; <span class="keyword">from</span> <span class="string">"react-transition-group"</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> withRouter(</span><br><span class="line">  (&#123; location &#125;) =&gt;</span><br><span class="line">    &lt;TransitionGroup&gt;</span><br><span class="line">      &lt;CSSTransition key=&#123;location.key&#125; classNames=<span class="string">"fade"</span> timeout=&#123;<span class="number">300</span>&#125;&gt;</span><br><span class="line">        &#123; children &#125;</span><br><span class="line">      &lt;<span class="regexp">/CSSTransition&gt;</span></span><br><span class="line"><span class="regexp">    &lt;/</span>TransitionGroup&gt;</span><br><span class="line">)</span><br></pre></td></tr></table></figure>
<blockquote>
<p>上面用到的 <code>fade</code> 样式，可以在 <code>src</code> 下的 <code>global.css</code> 里定义：</p>
</blockquote>
<figure class="highlight css"><table><tr><td class="code"><pre><span class="line"><span class="selector-class">.fade-enter</span> &#123;</span><br><span class="line">  <span class="attribute">opacity</span>: <span class="number">0</span>;</span><br><span class="line">  <span class="attribute">z-index</span>: <span class="number">1</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-class">.fade-enter</span><span class="selector-class">.fade-enter-active</span> &#123;</span><br><span class="line">  <span class="attribute">opacity</span>: <span class="number">1</span>;</span><br><span class="line">  <span class="attribute">transition</span>: opacity <span class="number">250ms</span> ease-in;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="4-5-面包屑"><a href="#4-5-面包屑" class="headerlink" title="4.5 面包屑"></a>4.5 面包屑</h2><blockquote>
<p>面包屑也是有多种实现方式，这里举 <code>react-router-breadcrumbs-hoc</code> 的例子。</p>
</blockquote>
<p>先安装依赖，</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">$ yarn add react-router-breadcrumbs-hoc</span><br></pre></td></tr></table></figure>
<p>然后实现一个 <code>Breakcrumbs.js</code>，比如：</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> NavLink <span class="keyword">from</span> <span class="string">'umi/navlink'</span>;</span><br><span class="line"><span class="keyword">import</span> withBreadcrumbs <span class="keyword">from</span> <span class="string">'react-router-breadcrumbs-hoc'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 更多配置请移步 https://github.com/icd2k3/react-router-breadcrumbs-hoc</span></span><br><span class="line"><span class="keyword">const</span> routes = [</span><br><span class="line">  &#123; <span class="attr">path</span>: <span class="string">'/'</span>, <span class="attr">breadcrumb</span>: <span class="string">'首页'</span> &#125;,</span><br><span class="line">  &#123; <span class="attr">path</span>: <span class="string">'/list'</span>, <span class="attr">breadcrumb</span>: <span class="string">'List Page'</span> &#125;,</span><br><span class="line">];</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> withBreadcrumbs(routes)(<span class="function">(<span class="params">&#123; breadcrumbs &#125;</span>) =&gt;</span> (</span><br><span class="line">  &lt;div&gt;</span><br><span class="line">    &#123;breadcrumbs.map(<span class="function">(<span class="params">breadcrumb, index</span>) =&gt;</span> (</span><br><span class="line">      &lt;span key=&#123;breadcrumb.key&#125;&gt;</span><br><span class="line">        &lt;NavLink to=&#123;breadcrumb.props.match.url&#125;&gt;</span><br><span class="line">          &#123;breadcrumb&#125;</span><br><span class="line">        &lt;<span class="regexp">/NavLink&gt;</span></span><br><span class="line"><span class="regexp">        &#123;(index &lt; breadcrumbs.length - 1) &amp;&amp; &lt;i&gt; /</span> &lt;<span class="regexp">/i&gt;&#125;</span></span><br><span class="line"><span class="regexp">      &lt;/</span>span&gt;</span><br><span class="line">    ))&#125;</span><br><span class="line">  &lt;<span class="regexp">/div&gt;</span></span><br><span class="line"><span class="regexp">));</span></span><br></pre></td></tr></table></figure>
<p>然后在需要的地方引入此 <code>React</code> 组件即可。</p>
<h2 id="4-6-启用-Hash-路由"><a href="#4-6-启用-Hash-路由" class="headerlink" title="4.6 启用 Hash 路由"></a>4.6 启用 Hash 路由</h2><blockquote>
<p><code>umi</code> 默认是用的 <code>Browser History</code>，如果要用 <code>Hash History</code>，需配置：</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  history: <span class="string">'hash'</span>,</span><br><span class="line">&#125;</span><br><span class="line"># Scroll to top</span><br><span class="line">在 layout 组件（layouts/index.js 或者 pages 子目录下的 _layout.js）的 componentDidUpdate 里决定是否 scroll to top，比如：</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> &#123; Component &#125; <span class="keyword">from</span> <span class="string">'react'</span>;</span><br><span class="line"><span class="keyword">import</span> withRouter <span class="keyword">from</span> <span class="string">'umi/withRouter'</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Layout</span> <span class="keyword">extends</span> <span class="title">Component</span> </span>&#123;</span><br><span class="line">  componentDidUpdate(prevProps) &#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">this</span>.props.location !== prevProps.location) &#123;</span><br><span class="line">      <span class="built_in">window</span>.scrollTo(<span class="number">0</span>, <span class="number">0</span>);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  render() &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">this</span>.props.children;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> withRouter(Layout);</span><br></pre></td></tr></table></figure>
<h2 id="4-7-页面间跳转"><a href="#4-7-页面间跳转" class="headerlink" title="4.7 页面间跳转"></a>4.7 页面间跳转</h2><blockquote>
<p>在 umi 里，页面之间跳转有两种方式：声明式和命令式</p>
</blockquote>
<p><strong>声明式</strong></p>
<blockquote>
<p>基于 <code>umi/link</code>，通常作为 <code>React</code> 组件使用。</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> Link <span class="keyword">from</span> <span class="string">'umi/link'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> () =&gt; (</span><br><span class="line">  &lt;Link to=<span class="string">"/list"</span>&gt;Go to list page&lt;<span class="regexp">/Link&gt;</span></span><br><span class="line"><span class="regexp">);</span></span><br></pre></td></tr></table></figure>
<p><strong>命令式</strong></p>
<blockquote>
<p>基于 <code>umi/router</code>，通常在事件处理中被调用。</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> router <span class="keyword">from</span> <span class="string">'umi/router'</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">goToListPage</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  router.push(<span class="string">'/list'</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="五、配置"><a href="#五、配置" class="headerlink" title="五、配置"></a>五、配置</h1><blockquote>
<p>配置文件<br><code>umi</code> 允许在 <code>.umirc.js</code> 或 <code>config/config.js</code>（二选一，<code>.umirc.js</code> 优先）中进行配置，支持 <code>ES6</code>语法。</p>
</blockquote>
<p>比如：</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  base: <span class="string">'/admin/'</span>,</span><br><span class="line">  publicPath: <span class="string">'http://cdn.com/foo'</span>,</span><br><span class="line">  plugins: [</span><br><span class="line">    [<span class="string">'umi-plugin-react'</span>, &#123;</span><br><span class="line">      dva: <span class="literal">true</span>,</span><br><span class="line">    &#125;],</span><br><span class="line">  ],</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p><strong>.umirc.local.js</strong></p>
<blockquote>
<p><code>.umirc.local.js</code> 是本地的配置文件，不要提交到 <code>git</code>，所以通常需要配置到 <code>.gitignore</code>。如果存在，会和 <code>.umirc.js</code> 合并后再返回。</p>
</blockquote>
<p><strong>UMI_ENV</strong></p>
<blockquote>
<p>可以通过环境变量 <code>UMI_ENV</code> 区分不同环境来指定配置。</p>
</blockquote>
<p>举个例子，</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">// .umirc.js</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123; <span class="attr">a</span>: <span class="number">1</span>, <span class="attr">b</span>: <span class="number">2</span> &#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// .umirc.cloud.js</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123; <span class="attr">b</span>: <span class="string">'cloud'</span>, <span class="attr">c</span>: <span class="string">'cloud'</span> &#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// .umirc.local.js</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123; <span class="attr">c</span>: <span class="string">'local'</span> &#125;;</span><br></pre></td></tr></table></figure>
<p>不指定 <code>UMI_ENV</code> 时，拿到的配置是：</p>
<figure class="highlight"><table><tr><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  a: 1,</span><br><span class="line">  b: 2,</span><br><span class="line">  c: 'local',</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>指定 <code>UMI_ENV=cloud</code>时，拿到的配置是：</p>
</blockquote>
<figure class="highlight"><table><tr><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  a: 1,</span><br><span class="line">  b: 'cloud',</span><br><span class="line">  c: 'local',</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="六、Mock-数据"><a href="#六、Mock-数据" class="headerlink" title="六、Mock 数据"></a>六、Mock 数据</h1><p><strong>使用 umi 的 mock 功能</strong></p>
<blockquote>
<p><code>umi</code> 里约定 <code>mock</code> 文件夹下的文件即<code>mock</code>文件，文件导出接口定义，支持基于 <code>require</code> 动态分析的实时刷新，支持 ES6 语法，以及友好的出错提示</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  <span class="comment">// 支持值为 Object 和 Array</span></span><br><span class="line">  <span class="string">'GET /api/users'</span>: &#123; <span class="attr">users</span>: [<span class="number">1</span>, <span class="number">2</span>] &#125;,</span><br><span class="line"></span><br><span class="line">  <span class="comment">// GET POST 可省略</span></span><br><span class="line">  <span class="string">'/api/users/1'</span>: &#123; <span class="attr">id</span>: <span class="number">1</span> &#125;,</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 支持自定义函数，API 参考 express@4</span></span><br><span class="line">  <span class="string">'POST /api/users/create'</span>: <span class="function">(<span class="params">req, res</span>) =&gt;</span> &#123; res.end(<span class="string">'OK'</span>); &#125;,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>当客户端（浏览器）发送请求，如：<code>GET /api/users</code>，那么本地启动的 <code>umi dev</code> 会跟此配置文件匹配请求路径以及方法，如果匹配到了，就会将请求通过配置处理</p>
</blockquote>
<p><strong>引入 Mock.js</strong></p>
<blockquote>
<p><code>Mock.js</code> 是常用的辅助生成模拟数据的第三方库，当然你可以用你喜欢的任意库来结合 roadhog 构建数据模拟功能</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> mockjs <span class="keyword">from</span> <span class="string">'mockjs'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  <span class="comment">// 使用 mockjs 等三方库</span></span><br><span class="line">  <span class="string">'GET /api/tags'</span>: mockjs.mock(&#123;</span><br><span class="line">    <span class="string">'list|100'</span>: [&#123; <span class="attr">name</span>: <span class="string">'@city'</span>, <span class="string">'value|1-100'</span>: <span class="number">50</span>, <span class="string">'type|0-2'</span>: <span class="number">1</span> &#125;],</span><br><span class="line">  &#125;),</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p><strong>添加跨域请求头</strong></p>
<blockquote>
<p>设置 <code>response</code> 的请求头即可：</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="string">'POST /api/users/create'</span>: <span class="function">(<span class="params">req, res</span>) =&gt;</span> &#123;</span><br><span class="line">  ...</span><br><span class="line">  res.setHeader(<span class="string">'Access-Control-Allow-Origin'</span>, <span class="string">'*'</span>);</span><br><span class="line">  ...</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>
<p><strong>合理的拆分你的 mock 文件</strong></p>
<blockquote>
<p>对于整个系统来说，请求接口是复杂并且繁多的，为了处理大量模拟请求的场景，我们通常把每一个数据模型抽象成一个文件，统一放在 mock 的文件夹中，然后他们会自动被引入</p>
</blockquote>
<p><img src="https://gw.alipayobjects.com/zos/rmsportal/wbeiDacBkchXrTafasBy.png" alt></p>
<p><strong>模拟延迟</strong></p>
<blockquote>
<p>为了更加真实的模拟网络数据请求，往往需要模拟网络延迟时间</p>
</blockquote>
<ul>
<li>手动添加 <code>setTimeout</code> 模拟延迟</li>
</ul>
<p>你可以在重写请求的代理方法，在其中添加模拟延迟的处理，如：</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="string">'POST /api/forms'</span>: <span class="function">(<span class="params">req, res</span>) =&gt;</span> &#123;</span><br><span class="line">  setTimeout(<span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">    res.send(<span class="string">'Ok'</span>);</span><br><span class="line">  &#125;, <span class="number">1000</span>);</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>
<p><strong>使用插件模拟延迟</strong></p>
<blockquote>
<p>上面的方法虽然简便，但是当你需要添加所有的请求延迟的时候，可能就麻烦了，不过可以通过第三方插件来简化这个问题，如：<code>roadhog-api-doc#delay</code>。</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; delay &#125; <span class="keyword">from</span> <span class="string">'roadhog-api-doc'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> proxy = &#123;</span><br><span class="line">  <span class="string">'GET /api/project/notice'</span>: getNotice,</span><br><span class="line">  <span class="string">'GET /api/activities'</span>: getActivities,</span><br><span class="line">  <span class="string">'GET /api/rule'</span>: getRule,</span><br><span class="line">  <span class="string">'GET /api/tags'</span>: mockjs.mock(&#123;</span><br><span class="line">    <span class="string">'list|100'</span>: [&#123; <span class="attr">name</span>: <span class="string">'@city'</span>, <span class="string">'value|1-100'</span>: <span class="number">50</span>, <span class="string">'type|0-2'</span>: <span class="number">1</span> &#125;]</span><br><span class="line">  &#125;),</span><br><span class="line">  <span class="string">'GET /api/fake_list'</span>: getFakeList,</span><br><span class="line">  <span class="string">'GET /api/fake_chart_data'</span>: getFakeChartData,</span><br><span class="line">  <span class="string">'GET /api/profile/basic'</span>: getProfileBasicData,</span><br><span class="line">  <span class="string">'GET /api/profile/advanced'</span>: getProfileAdvancedData,</span><br><span class="line">  <span class="string">'POST /api/register'</span>: <span class="function">(<span class="params">req, res</span>) =&gt;</span> &#123;</span><br><span class="line">    res.send(&#123; <span class="attr">status</span>: <span class="string">'ok'</span> &#125;);</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="string">'GET /api/notices'</span>: getNotices,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 调用 delay 函数，统一处理</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> delay(proxy, <span class="number">1000</span>);</span><br></pre></td></tr></table></figure>
<p><strong>联调</strong></p>
<blockquote>
<p>当本地开发完毕之后，如果服务器的接口满足之前的约定，那么你只需要不开本地代理或者重定向代理到目标服务器就可以访问真实的服务端数据，非常方便</p>
</blockquote>
<h1 id="七、结合dva实践"><a href="#七、结合dva实践" class="headerlink" title="七、结合dva实践"></a>七、结合dva实践</h1><blockquote>
<p>自<code>&gt;= umi@2</code>起，<code>dva</code>的整合可以直接通过 <code>umi-plugin-react</code> 来配置</p>
</blockquote>
<p><strong>特性</strong></p>
<ul>
<li>按目录约定注册 <code>model</code>，无需手动 <code>app.model</code></li>
<li>文件名即<code>namespace</code>，可以省去 model 导出的 <code>namespace key</code></li>
<li>无需手写 <code>router.js</code>，交给 <code>umi</code> 处理，支持 <code>model</code> 和 <code>component</code> 的按需加载</li>
<li>内置 <code>query-string</code> 处理，无需再手动解码和编码</li>
<li>内置 <code>dva-loading</code>和 <code>dva-immer</code>，其中 <code>dva-immer</code> 需通过配置开启</li>
<li>开箱即用，无需安装额外依赖，比如 <code>dva</code>、<code>dva-loading</code>、<code>dva-immer</code>、<code>path-to-regexp</code>、<code>object-assign</code>、<code>react</code>、<code>react-dom</code> 等`</li>
</ul>
<p><strong>使用</strong></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">$ yarn add umi-plugin-react</span><br></pre></td></tr></table></figure>
<p>然后在 <code>.umirc.js</code> 里配置插件：</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  plugins: [</span><br><span class="line">    [</span><br><span class="line">      <span class="string">'umi-plugin-react'</span>,</span><br><span class="line">    ]</span><br><span class="line">  ],</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>推荐开启 <code>dva-immer</code> 以简化 <code>reducer</code> 编写，</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  plugins: [</span><br><span class="line">    [</span><br><span class="line">      <span class="string">'umi-plugin-react'</span>,</span><br><span class="line">      &#123;</span><br><span class="line">        dva: &#123;</span><br><span class="line">          immer: <span class="literal">true</span></span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    ],</span><br><span class="line">  ],</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p><strong>model 注册</strong></p>
<blockquote>
<p><code>model</code> 分两类，一是全局<code>model</code>，二是页面<code>model</code>。全局 <code>model</code>存于 <code>/src/models/</code> 目录，所有页面都可引用；页面 model 不能被其他页面所引用。</p>
</blockquote>
<p>规则如下：</p>
<ul>
<li><code>src/models/**/*.js</code> 为 <code>global model</code></li>
<li><code>src/pages/**/models/**/*.js</code>为 <code>page model</code></li>
<li><code>global model</code> 全量载入，<code>page model</code> 在 <code>production</code>时按需载入，在 <code>development</code> 时全量载入</li>
<li><code>page model</code> 为 <code>page js</code> 所在路径下 <code>models/**/*.js</code> 的文件</li>
<li><code>page model</code> 会向上查找，比如 <code>page js</code> 为 <code>pages/a/b.js</code>，他的 <code>page model</code> 为 <code>pages/a/b/models/**/*.js</code> +<code>pages/a/models/**/*.js</code>，依次类推</li>
<li>约定 <code>model.js</code> 为单文件 model，解决只有一个<code>model</code> 时不需要建 models 目录的问题，有 <code>model.js</code>则不去找 <code>models/**/*.js</code></li>
</ul>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">+ src</span><br><span class="line">  + models</span><br><span class="line">    - g.js</span><br><span class="line">  + pages</span><br><span class="line">    + a</span><br><span class="line">      + models</span><br><span class="line">        - a.js</span><br><span class="line">        - b.js</span><br><span class="line">        + ss</span><br><span class="line">          - s.js</span><br><span class="line">      - page.js</span><br><span class="line">    + c</span><br><span class="line">      - model.js</span><br><span class="line">      + d</span><br><span class="line">        + models</span><br><span class="line">          - d.js</span><br><span class="line">        - page.js</span><br><span class="line">      - page.js</span><br></pre></td></tr></table></figure>
<p>如上目录：</p>
<ul>
<li><code>global model</code> 为 <code>src/models/g.js</code></li>
<li><code>/a</code> 的 <code>page model</code>为 <code>src/pages/a/models/{a,b,ss/s}.js</code></li>
<li><code>/c</code>的 <code>page model</code> <code>为</code> src/pages/c/model.js`</li>
<li><code>/c/d</code> 的 <code>page model</code> 为 <code>src/pages/c/model.js</code>, <code>src/pages/c/d/models/d.js</code></li>
</ul>
<h1 id="八、问题汇总"><a href="#八、问题汇总" class="headerlink" title="八、问题汇总"></a>八、问题汇总</h1><p><strong>1、如何配置 onError、initialState 等 hook？</strong></p>
<blockquote>
<p>新建 <code>src/dva.js</code>，通过导出的 <code>config</code> 方法来返回额外配置项，比如：</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; message &#125; <span class="keyword">from</span> <span class="string">'antd'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">config</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> &#123;</span><br><span class="line">    onError(err) &#123;</span><br><span class="line">      err.preventDefault();</span><br><span class="line">      message.error(err.message);</span><br><span class="line">    &#125;,</span><br><span class="line">    initialState: &#123;</span><br><span class="line">      global: &#123;</span><br><span class="line">        text: <span class="string">'hi umi + dva'</span>,</span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>2、url 变化了，但页面组件也刷新，是什么原因？</strong></p>
<blockquote>
<p><code>layouts/index.js</code> 里如果用了 <code>connect</code> 传数据，需要用 <code>umi/withRouter</code> 高阶一下</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> withRouter <span class="keyword">from</span> <span class="string">'umi/withRouter'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> withRouter(connect(mapStateToProps)(LayoutComponent));</span><br></pre></td></tr></table></figure>
<p><strong>3、如何访问到 store 或 dispatch 方法？</strong></p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="built_in">window</span>.g_app._store</span><br><span class="line"><span class="built_in">window</span>.g_app._store.dispatch</span><br></pre></td></tr></table></figure>
<p><strong>4、如何禁用包括 component 和 models 的按需加载？</strong></p>
<blockquote>
<p>在 <code>.umirc.js</code> 里配置：</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  disableDynamicImport: <span class="literal">true</span>,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p><strong>如果不用page.js的命名，倒是能生成路由，但是model、service、components就全部变路由了</strong></p>
<blockquote>
<p>不用 page.js，然后通过 umi-plugin-routes 过滤掉不需要的路由，参考 <a href="https://github.com/zuiidea/antd-admin/blob/develop/.umirc.js#L4-L16" target="_blank" rel="noopener">https://github.com/zuiidea/antd-admin/blob/develop/.umirc.js#L4-L16</a></p>
</blockquote>
<p><strong>.umirc.mock.js 这个文件怎么配置呢？</strong></p>
<blockquote>
<p>可以不用配置，在 mock/ 下建文件写 mock 代码即可。</p>
</blockquote>
<h1 id="九、Demo"><a href="#九、Demo" class="headerlink" title="九、Demo"></a>九、Demo</h1><blockquote>
<p><a href="https://github.com/poetries/umi-tmp" target="_blank" rel="noopener">https://github.com/poetries/umi-tmp</a></p>
</blockquote>
<h1 id="十、参考"><a href="#十、参考" class="headerlink" title="十、参考"></a>十、参考</h1><ul>
<li><a href="https://umijs.org/zh/config/#%E5%9F%BA%E6%9C%AC%E9%85%8D%E7%BD%AE" target="_blank" rel="noopener">Umi config配置</a></li>
<li><a href="https://umijs.org/zh/api/" target="_blank" rel="noopener">Umi APi</a></li>
<li><a href="https://umijs.org/zh/plugin/" target="_blank" rel="noopener">Umi插件</a></li>
<li><a href="https://github.com/sorrycc/blog/issues/66" target="_blank" rel="noopener">使用 umi 改进 dva 项目开发</a></li>
</ul>

      </div>
    
  </div>

</article>

<button class="assist-btn2 circle" id="assist_btn2" title="点亮屏幕" style="left: 27px; top: 152px;">
  <i class="iconfont" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>
<button class="assist-btn1 circle" id="assist_btn1" title="关闭屏幕亮度" style="left: 27px; top: 152px;">
  <i class="iconfont toc-title" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>


<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>	

<script src="https://my.openwrite.cn/js/readmore.js" type="text/javascript"></script>
<script>
  const btw = new BTWPlugin();
  btw.init({
    id: "container",
    blogId: "22699-1592137983091-414",
    name: "前端进阶之旅",
    qrcode: "https://poetries1.gitee.io/img-repo/2020/06/qrcode.jpg",
    keyword: "3a3b3c",
  });
</script>

<script type="text/javascript">

// white theme
var body = {color: "#555", background: "#000"};
var a_tag = {color: "#222"};
var header = { background: "#222"};
var logo_line_i = {background: "#222"};
// var post_code = {background: "#eee", color: "#222"};

function switch_theme() {
 $("body").css(body);
 $("a:not('.links-of-author-item a, .site-state-item a, .site-state-posts a, .feed-link a, .motion-element a, .post-tags a, .show-commit-cls a, #donate_board a')").css(a_tag);
 $(".header, .footer").css(header);
 $(".logo-line-before i, .logo-line-after i").css(logo_line_i);
 //$(".post code").css(post_code);
 $("#idhyt-surprise-ball #idhyt-surprise-ball-animation .drag").css(a_tag);
 $(".post-title-link, .posts-expand .post-meta, .post-comments-count, .disqus-comment-count, .post-category a, .post-nav-next a, .post-nav-item a").css(a_tag);
 
 // $("code").css({color: '#c5c8c6', background: '#1d1f21'});
 //$("#assist_btn1").hide(1500);
}

$(function () {
$("#assist_btn2").css("display","none");
 $("#assist_btn1").click(function() {
     switch_theme();
$("div#toc.toc-article").css({
 "background":"#eaeaea",
 "opacity":1
});
$(".toc-article ol").show();
$("#toc.toc-article .toc-title").css("color","#a98602");
$("#assist_btn1").css("display","none");
$("#assist_btn2").css("display","block");
 });
$("#assist_btn2").click(function() {
$("#assist_btn2").css("display","none");
$("#assist_btn1").css("display","block");
$("body").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
     $(".header, .footer").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
$(".toc-article ol").toggle(1000);
 });
});


//背景随机

var Y, O, E, L, B, C, T, z, N, S, A, I;
!function() {
var e = function() {
for (O.clearRect(0, 0, L, B), T = [{
x: 0,
y: .7 * B + C
}, {
x: 0,
y: .7 * B - C
}]; T[1].x < L + C;) t(T[0], T[1])
}, t = function(e, t) {
O.beginPath(), O.moveTo(e.x, e.y), O.lineTo(t.x, t.y);
var n = t.x + (2 * I() - .25) * C,
 r = a(t.y);
O.lineTo(n, r), O.closePath(), N -= S / -50, O.fillStyle = "#" + (127 * A(N) + 128 << 16 | 127 * A(N + S / 3) + 128 << 8 | 127 * A(N + S / 3 * 2) + 128).toString(16), O.fill(), T[0] = T[1], T[1] = {
 x: n,
 y: r
}
}, a = function n(e) {
var t = e + (2 * I() - 1.1) * C;
return t > B || t < 0 ? n(e) : t
};
Y = document.getElementById("evanyou"), O = Y.getContext("2d"), E = window.devicePixelRatio || 1, L = window.innerWidth, B = window.innerHeight, C = 90, z = Math, N = 0, S = 2 * z.PI, A = z.cos, I = z.random, Y.width = L * E, Y.height = B * E, O.scale(E, E), O.globalAlpha = .6, document.onclick = e, document.ontouchstart = e, e()
}()

   
$("#toc-eye").click(function(){
$("#toc.toc-article").toggle(1000);
});

</script>


   
  <div class="text-center donation">
    <div class="inner-donation">
      <span class="btn-donation">支持一下</span>
      <div class="donation-body">
        <div class="tip text-center">扫一扫，支持poetries</div>
        <ul>
        
          <li class="item">
            
              <span>微信扫一扫</span>
            
            <img src="/images/weixin.jpg" alt="">
          </li>
        
          <li class="item">
            
              <span>支付宝扫一扫</span>
            
            <img src="/images/zhifubao.jpg" alt="">
          </li>
        
        </ul>
      </div>
    </div>
  </div>


   
  <div class="box-prev-next clearfix">
    <a class="show pull-left" href="/2018/09/05/dva/">
        <i class="icon icon-angle-left"></i>
    </a>
    <a class="show pull-right" href="/2018/09/16/ant-design-pro/">
        <i class="icon icon-angle-right"></i>
    </a>
  </div>




</div>


  <a id="backTop" class="back-top">
    <i class="icon-angle-up"></i>
  </a>




  <div class="modal" id="modal">
  <span id="cover" class="cover hide"></span>
  <div id="modal-dialog" class="modal-dialog hide-dialog">
    <div class="modal-header">
      <span id="close" class="btn-close">关闭</span>
    </div>
    <hr>
    <div class="modal-body">
      <ul class="list-toolbox">
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/archives/"
              rel="noopener noreferrer"
              target="_self"
              >
              博客
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/categories/"
              rel="noopener noreferrer"
              target="_self"
              >
              分类
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/tags/"
              rel="noopener noreferrer"
              target="_self"
              >
              标签
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/search/"
              rel="noopener noreferrer"
              target="_self"
              >
              搜索
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/link/"
              rel="noopener noreferrer"
              target="_self"
              >
              友链
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/about/"
              rel="noopener noreferrer"
              target="_self"
              >
              关于
            </a>
          </li>
        
      </ul>

    </div>
  </div>
</div>



  
      <div class="fexo-comments comments-post">
    

    

    
    

    

    
    

    

<!-- Gitalk评论插件通用代码 -->
<div id="gitalk-container"></div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">
<script src="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
<script>
const gitalk = new Gitalk({
  clientID: '5567a2c4abb858009d96',
  clientSecret: 'b9039ec056cf5c2346b3cdb63308a28c163f91e5',
  repo: 'poetries.github.io',
  owner: 'poetries',
  // 在这里设置一下截取前50个字符串, 这是因为 github 对 label 的长度有了要求, 如果超过
  // 50个字符串则会报错.
  // id: location.pathname.split('/').pop().substring(0, 49),
  id: location.pathname,
  admin: ['poetries'],
  // facebook-like distraction free mode
  distractionFreeMode: false
})
gitalk.render('gitalk-container')
</script>
<!-- Gitalk代码结束 -->



  </div>

  

  <script type="text/javascript">
  function loadScript(url, callback) {
    var script = document.createElement('script')
    script.type = 'text/javascript';

    if (script.readyState) { //IE
      script.onreadystatechange = function() {
        if (script.readyState == 'loaded' ||
          script.readyState == 'complete') {
          script.onreadystatechange = null;
          callback();
        }
      };
    } else { //Others
      script.onload = function() {
        callback();
      };
    }

    script.src = url;
    document.getElementsByTagName('head')[0].appendChild(script);
  }

  window.onload = function() {
    loadScript('/js/bundle.js?235683', function() {
      // load success
    });
  }
</script>


  <!-- 页面点击小红心 -->
  <script type="text/javascript" src="/js/clicklove.js"></script>
 
  
</body>
</html>
